package meterMonitor.biz;

import meterMonitor.framework.context.CurrentUser;
import meterMonitor.mapper.AuthMapper;
import meterMonitor.mapper.UserAuthMapper;
import meterMonitor.mapper.UserMapper;
import meterMonitor.model.User;
import meterMonitor.model.UserAuth;
import meterMonitor.vo.UserExtend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserBiz {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserAuthMapper userAuthMapper;

    @Autowired
    private AuthMapper authMapper;

    /**
     * login(登录查询)
     *
     * @param loginName
     * @param loginPassword
     * @return User
     * @throws
     */
    public User login(String loginName, String loginPassword) {
        User user = new User();
        user.setLoginName(loginName);
        User login = userMapper.loginQuery(user);
        //if(login != null && MuggleUtil.parse(login.getPassword()).equals(loginPassword)){
        if (login != null && login.getPassword().equals(loginPassword)) {
            return login;
        } else {
            return null;
        }
    }

    public List<String> getLoginNames() {
        return userMapper.selectAllLoginName();
    }

    public List<String> queryAuthCode(String loginName) {
        List<UserAuth> list = queryUserAuth(loginName);
        List<String> authList = new ArrayList<String>();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                String authCode = list.get(i).getAuthCode();
//                if (authCode.equals("edit")){
//                    authCode="设置";
//                }if (authCode.equals("query")){
//                    authCode="查询";
//                } if (authCode.equals("system_auth")){
//                authCode="高级权限";
//            }
                authList.add(authCode);
            }
        }
        return authList;
    }

    public List<UserAuth> queryUserAuth(String loginName) {
        return userAuthMapper.queryUserAuth(loginName);
    }

    public void insetUser(CurrentUser currentUser) {
        userMapper.addUser(currentUser);
    }

    //查询全部用户以及所对应的权限;User 存放权限拼接字符串
    public List<User> selectAllUser() {
        List<User> list = userMapper.selectAllUser();
        for (User user : list) {
            //List<String> listStr = queryAuthCode(user.getLoginName());
            List<String> listStr = getAuthList(user.getLoginName());
            String auth = listToString(listStr);
            user.setCompanyName(auth);
        }
        return list;
    }

    //根据loginName查询权限集合
    private List<String> getAuthList(String loginName) {
        return authMapper.selectAuths(loginName);
    }

    /**
     * 像用户表更新用户信息；向用户权限表中加入用户权限信息
     *
     * @param userExtend
     */
    public void setUser(UserExtend userExtend) {
        User user = new User();
        user.setLoginName(userExtend.getLoginName());
        user.setPassword(userExtend.getPassWord());
        user.setRemark(userExtend.getPhoneNo() + "");
        user.setLastModifyTime(new Date());
        userMapper.updateByLoginNameSelective(user);


        userAuthMapper.deleteByLoginName(userExtend.getLoginName());
        List<UserAuth> list = new ArrayList<>();

        String auth = userExtend.getAuths();
        String[] arr = auth.split(",");
        for (String str : arr) {
            UserAuth userAuth = new UserAuth();
            userAuth.setAuthCode(str);
            userAuth.setLoginName(userExtend.getLoginName());
            list.add(userAuth);
        }
        userAuthMapper.insertUserAuth(list);

    }

    //删除用户
    public void deleteUser(User user) {
        userAuthMapper.deleteByLoginName(user.getLoginName());
        userMapper.deleteLoginName(user.getLoginName());
    }

    //集合转字符串
    private String listToString(List<String> list) {

        if (list == null) {
            return null;
        }

        StringBuilder result = new StringBuilder();
        boolean first = true;

        //第一个前面不拼接","
        for (String string : list) {
            if (first) {
                first = false;
            } else {
                result.append(",");
            }
            result.append(string);
        }
        return result.toString();
    }

}
